import { defineStore } from 'pinia';
import { CustomRoute } from 'src/types/route'


/**
 * 标签视图存储
 */
export const useTagsViewStore = defineStore('tags-view', {
  state: () => ({
    /**
     * 访问过的视图
     */
    visitedViews: [] as CustomRoute[],
    /**
     * 缓存的视图名称
     */
    cachedViews: [] as string[],
    /**
     * iframe 视图
     */
    iframeViews: [] as CustomRoute[]
  }),
  actions: {
    /**
     * 添加一个视图到访问过的视图和缓存的视图
     * @param view - 要添加的视图
     */
    addView(view: CustomRoute) {
      this.addVisitedView(view);
      this.addCachedView(view);
    },
    /**
     * 添加一个 iframe 视图
     * @param view - 要添加的 iframe 视图
     */
    addIframeView(view: CustomRoute) {
      if (this.iframeViews.some(v => v.path === view.path)) return;
      this.iframeViews.push({
        ...view,
        meta:{
            title: view.meta?.title || 'no-name'
        }
      });
    },
    /**
     * 添加一个访问过的视图
     * @param view - 要添加的视图
     */
    addVisitedView(view: CustomRoute) {
      if (this.visitedViews.some(v => v.path === view.path)) return;
      this.visitedViews.push({
        ...view,
        meta: {
          title: view.meta?.title || 'no-name',
          affix: view.meta?.affix || false,
          noCache: view.meta?.noCache || false,
          link: view.meta?.link || false
        }
      });
    },
    /**
     * 添加一个视图到缓存
     * @param view - 要添加的视图
     */
    addCachedView(view: CustomRoute) {
      if (this.cachedViews.includes(view.name as string)) return;
      if (!view.meta?.noCache) {
        this.cachedViews.push(view.name as string);
      }
    },
    /**
     * 删除一个视图及其缓存
     * @param view - 要删除的视图
     * @returns 返回更新后的访问过的视图和缓存的视图
     */
    delView(view: CustomRoute): Promise<{ visitedViews: CustomRoute[]; cachedViews: string[] }> {
      return new Promise(resolve => {
        this.delVisitedView(view);
        this.delCachedView(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    /**
     * 删除一个访问过的视图
     * @param view - 要删除的视图
     * @returns 返回更新后的访问过的视图
     */
    delVisitedView(view: CustomRoute): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1);
            break;
          }
        }
        this.iframeViews = this.iframeViews.filter(item => item.path !== view.path);
        resolve([...this.visitedViews]);
      });
    },
    /**
     * 删除一个 iframe 视图
     * @param view - 要删除的 iframe 视图
     * @returns 返回更新后的 iframe 视图
     */
    delIframeView(view: CustomRoute): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        this.iframeViews = this.iframeViews.filter(item => item.path !== view.path);
        resolve([...this.iframeViews]);
      });
    },
    /**
     * 删除一个缓存的视图
     * @param view - 要删除的视图
     * @returns 返回更新后的缓存的视图
     */
    delCachedView(view: CustomRoute): Promise<string[]> {
      return new Promise(resolve => {
        const index = this.cachedViews.indexOf(view.name as string);
        if (index > -1) {
          this.cachedViews.splice(index, 1);
        }
        resolve([...this.cachedViews]);
      });
    },
    /**
     * 删除其他所有视图及其缓存
     * @param view - 当前保留的视图
     * @returns 返回更新后的访问过的视图和缓存的视图
     */
    delOthersViews(view: CustomRoute): Promise<{ visitedViews: CustomRoute[]; cachedViews: string[] }> {
      return new Promise(resolve => {
        this.delOthersVisitedViews(view);
        this.delOthersCachedViews(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    /**
     * 删除其他所有访问过的视图
     * @param view - 当前保留的视图
     * @returns 返回更新后的访问过的视图
     */
    delOthersVisitedViews(view: CustomRoute): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter(v => v.meta?.affix || v.path === view.path);
        this.iframeViews = this.iframeViews.filter(item => item.path === view.path);
        resolve([...this.visitedViews]);
      });
    },
    /**
     * 删除其他所有缓存的视图
     * @param view - 当前保留的视图
     * @returns 返回更新后的缓存的视图
     */
    delOthersCachedViews(view: CustomRoute): Promise<string[]> {
      return new Promise(resolve => {
        const index = this.cachedViews.indexOf(view.name as string);
        if (index > -1) {
          this.cachedViews = this.cachedViews.slice(index, index + 1);
        } else {
          this.cachedViews = [];
        }
        resolve([...this.cachedViews]);
      });
    },
    /**
     * 删除所有视图及其缓存
     * @returns 返回更新后的访问过的视图和缓存的视图
     */
    delAllViews(): Promise<{ visitedViews: CustomRoute[]; cachedViews: string[] }> {
      return new Promise(resolve => {
        this.delAllVisitedViews();
        this.delAllCachedViews();
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    /**
     * 删除所有访问过的视图
     * @returns 返回更新后的访问过的视图
     */
    delAllVisitedViews(): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        const affixTags = this.visitedViews.filter(tag => tag.meta?.affix);
        this.visitedViews = affixTags;
        this.iframeViews = [];
        resolve([...this.visitedViews]);
      });
    },
    /**
     * 删除所有缓存的视图
     * @returns 返回更新后的缓存的视图
     */
    delAllCachedViews(): Promise<string[]> {
      return new Promise(resolve => {
        this.cachedViews = [];
        resolve([...this.cachedViews]);
      });
    },
    /**
     * 更新一个访问过的视图
     * @param view - 要更新的视图
     */
    updateVisitedView(view: CustomRoute) {
      for (let v of this.visitedViews) {
        if (v.path === view.path) {
          v = { ...v, ...view };
          break;
        }
      }
    },
    /**
     * 删除右侧的所有标签
     * @param view - 当前保留的视图
     * @returns 返回更新后的访问过的视图
     */
    delRightTags(view: CustomRoute): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        const index = this.visitedViews.findIndex(v => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx <= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.cachedViews.indexOf(item.name as string);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta?.link) {
            const fi = this.iframeViews.findIndex(v => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    },
    /**
     * 删除左侧的所有标签
     * @param view - 当前保留的视图
     * @returns 返回更新后的访问过的视图
     */
    delLeftTags(view: CustomRoute): Promise<CustomRoute[]> {
      return new Promise(resolve => {
        const index = this.visitedViews.findIndex(v => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.visitedViews.filter((item, idx) => {
          if (idx >= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.cachedViews.indexOf(item.name as string);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta?.link) {
            const fi = this.iframeViews.findIndex(v => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    }
  }
});